﻿/*
 * This software is the confidential and proprietary
 * information of NetBay
 * Such confidential information should not be disclosed
 * NetBay copyright(c) 2010
 */

#region Using

using System.Collections.Generic;
using System.Runtime.InteropServices.Automation;

#endregion

namespace NetBay.System.Silverlight.Device
{
    public class Scanner
    {
        /// <summary>
        /// Return all scanners Device name and Device Id
        /// </summary>
        /// <returns>The list off all scanners. the dictinnary is composed of the Device ID and the Device Name</returns>
        public static Dictionary<string, string> GetScanners()
        {
            Dictionary<string, string> L_dicScanner = new Dictionary<string, string>();
            using (dynamic L_oDeviceManager = AutomationFactory.CreateObject("WIA.DeviceManager"))
            {
                var L_oDeviceInfos = L_oDeviceManager.DeviceInfos;
                for (int i = 1; i <= L_oDeviceInfos.Count; i++)
                {
                    var L_oIDevice = L_oDeviceInfos.Item(i).Connect();
                    var L_oDeviceID = L_oIDevice.DeviceID;
                    var L_oDeviceName = L_oIDevice.Properties("Name").Value;

                    L_dicScanner.Add(L_oDeviceName, L_oDeviceID);
                }
            }
            return L_dicScanner;
        }


        /// <summary>
        /// Lanch the scann command on the specified scanner
        /// </summary>
        /// <param name="p_sScannerGuid">The GUID of the scaner</param>
        public static void LaunchScann(string p_sScannerGuid)
        {
            using (dynamic DeviceManager = AutomationFactory.CreateObject("WIA.DeviceManager"))
            {
                var deviceInfos = DeviceManager.DeviceInfos;

                for (int i = 1; i <= deviceInfos.Count; i++)
                {
                    var L_oDevice = deviceInfos.Item(i).Connect();
                    string L_oDeviceID = L_oDevice.DeviceID;
                    if (p_sScannerGuid.Equals(L_oDeviceID))
                    {
                        dynamic item = L_oDevice.Items[1];
                        dynamic Devices = AutomationFactory.CreateObject("WIA.CommonDialog");
                        Devices.ShowTransfer(item);
                    }
                }
            }
        }
    }
}
